package com.zlp.partice;

import org.junit.Test;

import java.util.*;

class MapSum {

    Map<String,Integer>map = new HashMap<>();
    public void insert(String key, int val) {
        map.put(key,val);
    }

    public int sum(String prefix) {
        return map.keySet()
                .stream()
                .filter(o->o.startsWith(prefix))
                .mapToInt(o->map.get(o))
                .sum();
    }

    public static void main(String[] args) {
        System.out.println(findLHS(new int[]{1, 3, 2, 2, 5, 2, 3, 7}));
    }
    public static int findLHS(int[] nums) {
        Map<Integer,Integer>map = new HashMap<>();
        int ret = 0;
        for (int i=nums.length-1;i>=0;i--){
            map.put(nums[i],map.getOrDefault(nums[i],0)+1);
            int low = map.getOrDefault(nums[i]-1,0);
            int up = map.getOrDefault(nums[i]+1,0);
            if(low==0&&up==0) continue;
            ret = Math.max(ret,map.get(nums[i])+Math.max(up,low));
        }
        return ret;
    }

    int[] source;
    int[] operate;
    public void Solution(int[] nums) {
        source = nums;
        operate = new int[source.length];
        System.arraycopy(source,0,operate,0,source.length);
    }

    public int[] reset() {
        return source;
    }

    public int[] shuffle() {
        for (int i=0;i<source.length;i++){
            int cur = new Random().nextInt(source.length);
            int temp = operate[cur];
            operate[cur] = operate[i];
            operate[i] = temp;
        }
        return operate;
    }

    public boolean buddyStrings(String s, String goal) {
        int[] ch1 = new int[26],ch2 = new int[26];
        int ret = 0,flag = 0;
        for (int i=0;i<s.length();i++){
            ch2[goal.charAt(i)-'a'] ++;
            ch1[s.charAt(i)-'a'] ++;
            if(ch1[s.charAt(i)-'a']>1) flag = 1;
            if(s.charAt(i)!=goal.charAt(i)){
                ret++;
                if(ret>2) return false;
            }
        }
        for (int i=0;i<26;i++){
            if(ch1[i]!=ch2[i]) return false;
        }
        return ret == 2 || ret == 0 && flag == 1;
    }

    int[] num = {0,2,4,6,8,3,5,7,9,1};
    char[] ch = {'z','w','v','x','g','t','f','v','i','n'};
    String[] str = {"zero","two","five","six","eight","three","five","seven","nine","one"};
    public String originalDigits(String s) {
        StringBuilder sb = new StringBuilder();
        int[] map = new int[26];
        for (char ch:s.toCharArray()){
            map[ch-'a']++;
        }
        for (int i=0;i<10;i++){
            int cur = map[ch[i]-'a'];
            if(cur == 0) continue;
            for (int j=0;j<str[i].length();j++){
                map[str[i].charAt(j)-'a'] -= cur;
            }
            while (cur-->0) sb.append(num[i]);
        }
        char[] chars = sb.toString().toCharArray();
        Arrays.sort(chars);
        return new String(chars);
    }


}
